package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.entity.activity.ghost.GhostUserData;
import com.yanqu.road.entity.activity.ghost.config.GhostConfig;
import com.yanqu.road.logic.pb.GhostPb;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ghost.GhostModule;
import com.yanqu.road.server.manger.activity.ghost.GhostMgr;
import com.yanqu.road.server.manger.activity.ghost.GhostSceneLogic;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_GHOST_ENTER_BOSS_ROOM, desc = "进入boss房间（获取boss数据）")
public class GhostEnterBossRoomCmd extends AbstractCommand {

    private int bossId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.EnterBossRoomReqMsg reqMsg = GhostActivityProto.EnterBossRoomReqMsg.parseFrom(packet.getBytes());
        bossId = reqMsg.getBossId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        GhostConfig ghostConfig = GhostMgr.getGhostConfig();
        if (!GhostMgr.inOpenTime()) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        GhostMgr.checkUserData(player);

        GhostUserData userData = player.getModule(GhostModule.class).getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_SUOYAO_ACTIVITY_NO_UNLOCK;
            return;
        }

        // 进入房间
        GhostSceneLogic.enterRoom(player, bossId);
        // 跨服请求数据 + 进入房间
        GhostActivityProto.CrossEnterBossRoomReqMsg.Builder builder = GhostActivityProto.CrossEnterBossRoomReqMsg.newBuilder();
        builder.setActivityId(ghostConfig.getActivityId());
        builder.setPlayerData(GhostPb.parseGhostSimplePlayerMsg(
                player.getUserId(),
                player.getUserInfo().getOriginalServerId(),
                player.getUserInfo().getNickName(),
                player.getUserInfo().getDecoration()));
        builder.setBossId(bossId);
        builder.setUserResetTime(userData.getResetTime());
        player.sendPacket(CrossProtocol.C2_GHOST_ACTIVITY_ENTER_BOSS_ROOM, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            GhostActivityProto.EnterBossRoomRespMsg.Builder builder = GhostActivityProto.EnterBossRoomRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_GHOST_ENTER_BOSS_ROOM, builder);
        }
    }
}
